package com.asura.app.wechat.repository;

import com.asura.app.library.utils.ExecutorHelper;
import com.asura.app.wechat.repository.datasource.UserInfoDataSource;
import com.asura.app.wechat.repository.model.UserInfo;

import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Asura on 2017/12/28.
 */

public class UserInfoDataRepository implements UserInfoDataSource {


    private UserInfo userInfoMemoryCache;
    private final UserInfoRemoteDataSource userInfoRemoteDataSource;

    public UserInfoDataRepository() {
        userInfoRemoteDataSource = new UserInfoRemoteDataSource();
    }

    @Override
    public Single<UserInfo> getUserInfo() {
        if (userInfoMemoryCache != null) {
            return Single.just(userInfoMemoryCache);
        }
        return userInfoRemoteDataSource
                .getUserInfo()
                .doOnSuccess(userInfo -> {
                    userInfoMemoryCache = userInfo;
                })
                .subscribeOn(Schedulers.from(ExecutorHelper.HIGH));
    }
}
